package com.sphd.emanu.activity.materials;

import android.content.Context;
import android.util.Log;

import com.google.gson.JsonObject;
import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.CategoryAndStorageEntity;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.L;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * MVPPlugin
 *  邮箱 784787081@qq.com
 */

public class MaterialsPresenter extends BasePresenterImpl<MaterialsContract.View> implements MaterialsContract.Presenter{

    /**
     * @param org
     *  获取物料类型及库位的list
     */
    @Override
    public void categorAndStorage(int org,Integer id) {
        Call<String> entityCall=emanuService.categorAndStorage(org,id);
        entityCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.toString());
                CategoryAndStorageEntity categoryAndStorageEntity= GsonUtil.GsonToBean(response.body(),CategoryAndStorageEntity.class);
                mView.setEntity(categoryAndStorageEntity);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void updateMaterial(int id, String name, String specifications, Integer category, String minimum_stock, String stock, Integer storage) {
        Call<JsonObject> jsonObjectCall=emanuService.updateMaterial(id,name,specifications,category,minimum_stock,stock,storage);
        jsonObjectCall.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                L.e(response.toString());
                backRes(response);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }
    @Override
    public void updateMaterialNoStorage(int id, String name, String specifications, Integer category, String minimum_stock, String stock) {
        Call<JsonObject> jsonObjectCall=emanuService.updateMaterialNoStorage(id,name,specifications,category,minimum_stock,stock);
        jsonObjectCall.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {

                backRes(response);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void addMaterial(String name, String specifications, Integer category, String minimum_stock, String stock, Integer storage) {
        Call<JsonObject> jsonObjectCall=emanuService.addMaterial(name,specifications,category,minimum_stock,stock,storage);
        jsonObjectCall.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                backRes(response);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void adddateMaterialNoStorage(String name, String specifications, Integer category, String minimum_stock, String stock) {
        Call<JsonObject> jsonObjectCall=emanuService.adddateMaterialNoStorage(name,specifications,category,minimum_stock,stock);
        jsonObjectCall.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {

                backRes(response);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    private void backRes(Response<JsonObject> response) {
        L.e(response.toString());
        int res=res(response);
        mView.setRes(res);
    }


}
